package snoopy

import (
	"io/ioutil"
	"log"
	"net/http"
	"regexp"
	"strings"
)

type Snoopy struct {
	Url      string //要抓取的url
	Content  string //抓取的内容
	Mothod   string //请求类型 post get
	Url_data string //请求参数

}
type LinkData struct {
	url string
}

//抓取页面数据
func (this *Snoopy) GetContent() (bool, interface{}) {
	var (
		url string = this.Url
		h   *http.Response
		err error
	)
	if this.Mothod == "GET" {
		h, err = http.Get(url)
	} else {
		h, err = http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(this.Url_data))
	}

	defer h.Body.Close()
	if err != nil {
		log.Println(err)
		return false, err
	}
	data, err1 := ioutil.ReadAll(h.Body)
	if err1 != nil {
		return false, err
	} else {
		this.Content = string(data)
		return true, err1
	}

}

// 初始化包
func GetSonnpy(url, mothod, request_data string) (*Snoopy, interface{}) {
	if url == "" {
		return nil, "url not null"
	}
	if len(mothod) == 0 {
		mothod = "get"
	}

	if mothod != "get" && mothod != "post" {
		return nil, "Wrong request mode"
	}

	data := Snoopy{}
	data.Url = url
	data.Mothod = strings.ToUpper(mothod)
	data.Url_data = request_data
	return &data, nil
}

// 获取页面 url  limit 获取多少条
func (this *Snoopy) GetLink(limit int) (index []LinkData) {

	var link = regexp.MustCompile(`((http|ftp|https)://)(([a-zA-Z0-9\._-]+\.[a-zA-Z]{2,6})|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,4})*(/[a-zA-Z0-9\&%_\./-~-]*)?`)
	matches := link.FindAllStringSubmatch(this.Content, limit)
	index = make([]LinkData, len(matches))
	for i, item := range matches {
		index[i].url = item[0]
	}
	return index
}
